package javaDemo08.员工表处理;


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;


public class EmployeeInformation {
    private EmployeeInformation() {
    }

//    /**
//     * 步骤一  排序后写入
//     *
//     * @param infilepath  of String
//     * @param outfilepath of String
//     * @throws IOException
//     */
//    public static void one(String infilepath, String outfilepath) throws IOException {
//        ArrayList<Employee> list = input(infilepath);
//
//        Collections.sort(list);     //排序
//
//        output(outfilepath, list);   //写入
//
//    }

    /**
     * 步骤一至四  加薪后排序写入
     *
     * @param infilepath  of String
     * @param outfilepath of String
     */
    public static void oneToFour(String infilepath, String outfilepath) throws IOException {
        ArrayList<Employee> list = input(infilepath);

        for (Employee e : list) {
            date(e);               //加薪
        }

        Collections.sort(list);    //排序

        output(outfilepath, list);  //写入

    }

    /**
     * 步骤五 查询出2021年以后入职的员工信息
     * 默认包含2021年入职
     */
    public static void five(String infilepath, String outfilepath) throws IOException {
        ArrayList<Employee> list = input(infilepath);
        ArrayList<Employee> list2 = new ArrayList<>();

        //筛选2021年入职
        for (Employee e : list) {
            String[] strs = e.getData().split("-");
            int year = Integer.parseInt(strs[0]);
            if (year >= 2021) {
                list2.add(e);
            }
        }
        //筛选后加薪
        for(Employee e:list2){
            date(e);
        }
        Collections.sort(list);    //排序
        output(outfilepath, list2);
    }


    /**
     * 缓冲输入流读取文件
     *
     * @return Arraylist<Employee>
     */
    private static ArrayList<Employee> input(String infilepath) throws IOException {
        BufferedReader bfr = new BufferedReader(new FileReader(infilepath));
        ArrayList<Employee> list = new ArrayList<>();
        String str = bfr.readLine();

        while (str != null) {
            String[] strs = str.split("\t");
            list.add(new Employee(strs[0], strs[1], strs[2], strs[3], strs[4], strs[5]));
            str = bfr.readLine();
        }

        return list;
    }

    /**
     * 缓冲输出流写出数据
     *
     * @param outfilepath
     * @param list
     * @throws IOException
     */
    private static void output(String outfilepath, ArrayList list) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(outfilepath));

        for (Object e : list) {
            bw.write(e.toString());
            bw.newLine();
        }

        bw.close();

    }


    /**
     * 入职时间判断及加薪
     *
     * @param e
     */

    private static void date(Employee e) {
        String[] dates = e.getData().split("-");
        int a = Integer.parseInt(dates[0]);
        if (a <= 2010 && e.getGrade().equals("P2") && e.getWages() < 12000) {
            e.setWages(12000);
            e.setGrade("P3");
        } else if (a <= 2010 && e.getWages() < 12000) {
            e.setWages(12000);
        } else if (a <= 2020) {
            salary(e, 1.1);
        } else if (a == 2021) {
            salary(e, 100);
        }
    }

    /**
     * 百分比加薪
     *
     * @param e
     * @param SiziOfWages
     */
    private static void salary(Employee e, double SiziOfWages) {
        e.setWages((int) (e.getWages() * SiziOfWages));
    }

    /**
     * 直接加薪
     *
     * @param e
     * @param SiziOfWages
     */
    private static void salary(Employee e, int SiziOfWages) {
        e.setWages(e.getWages() + SiziOfWages);
    }


}
